package cqrtplm.controller;

import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.form.model.vo.PdmForm;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.ty.basic.constant.ApiPathConstant;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.utils.ArgumentUtils;
import com.ty.basic.utils.PageSizeUtil;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.dto.PdmFormDTO;
import cqrtplm.service.RTFormService;
import cqrtplm.vo.RTResultEntityVO;
import io.swagger.annotations.Api;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(ApiPathConstant.PRODUCT_VERSION_ONE_PATH + "form/")
@Api(tags = "历史数据导入")
public class RTFormController {

    @Resource
    private RTFormService rtFormService;

    @RequestMapping("queryBomByCondition")
    public ResponseResult<PageInfo<RTResultEntityVO>> queryBomByCondition(@RequestBody SearchConditionDTO conditionDTO) {
        try {
            conditionDTO.setPageSize(PageSizeUtil.getNormalSize(conditionDTO.getPageSize()));
            PageInfo<RTResultEntityVO> result = rtFormService.queryBomByCondition(conditionDTO);
            return ResponseResult.success(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取当前用户部门主管
     * @return
     */
    @GetMapping("getCurrentUserLeader")
    public ResponseResult getCurrentUserLeader() {
        try {
            String result = rtFormService.getCurrentUserLeader();
            return ResponseResult.success(result);
        } catch (Exception e) {
            throw RTErrorCodeEnum.USER_NOT_EXIST.getException(e.getMessage());
        }
    }

    @PostMapping("/newGetPdmFormWithDataByType")
    public ResponseResult<PdmForm> newGetPdmFormWithDataByType(@RequestBody PdmFormDTO dto) {
        try {
            PdmForm result = rtFormService.newGetPdmFormWithDataByType(dto);
            return ResponseResult.success(result);
        } catch (Exception e) {
            throw RTErrorCodeEnum.CLASSIFICATION_NOT_EXIST.getException(e.getMessage());
        }
    }

    @GetMapping("/selectMaterialAttrByClassId")
    public ResponseResult<List<String>> selectMaterialAttrByClassId(@Param("classId")String classId,
                                                                    @Param("type") String type) {
        try {
            List<String> result = rtFormService.selectMaterialAttrByClassId(classId, type);
            return ResponseResult.success(result);
        } catch (Exception e) {
            throw RTErrorCodeEnum.CLASSIFICATION_NOT_EXIST.getException(e.getMessage());
        }
    }

    @GetMapping("queryEBom")
    public ResponseResult<PartBomTree> queryEBom(@Param("number") String number, @Param("type") String type) {
        try {
            ArgumentUtils.checkArgumentBlank(number, "number");
            return ResponseResult.success(rtFormService.queryEBom(number, type));
        } catch (Exception e) {
            e.printStackTrace();
            throw RTErrorCodeEnum.CLASSIFICATION_NOT_EXIST.getException(e.getMessage());
        }
    }

    @GetMapping("queryProjectCode")
    public ResponseResult queryProjectCode() {
        try {
            return ResponseResult.success(rtFormService.queryProjectCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw RTErrorCodeEnum.CLASSIFICATION_NOT_EXIST.getException(e.getMessage());
        }
    }
}